package com.onLineCar.manages.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.manages.pojo.CarModel;
import com.onLineCar.manages.pojo.dto.CarModelDTO;
import com.onLineCar.manages.pojo.request.CarModelRequest;
import com.onLineCar.manages.service.CarModelService;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static com.onLineCar.manages.aspect.config.OperateLogAspect.userNameThreadLocal;

//import static com.onLineCar.manages.aspect.config.OperateLogAspect.userNameThreadLocal;

/**
 * <p>
 * 车型表 前端控制器
 * </p>
 *
 * @author zhucaiyi
 * @since 2023-12-10
 */
@NoArgsConstructor
@Data
@RestController
@RequestMapping("/car-model")
@Slf4j
public class CarModelController {

    @Resource
    private CarModelService carModelService;



    @GetMapping("/monit")
    public String monit() {
        return "monit";
    }

    /**
     * 列表
     *
     * @return
     */
    @GetMapping("/queryCarModel")
    public Result<IPage<CarModel>> queryCarModel(CarModelRequest carModelRequest) {
        return Result.success(carModelService.queryCarModel(carModelRequest));
    }

    /**
     * 全量数据
     *
     * @return
     */
    @GetMapping("/queryCarModelAll")
    public Result<List<CarModel>> queryCarModelAll() {
        return Result.success(carModelService.list(new LambdaQueryWrapper<CarModel>()
                .eq(CarModel::getStatus, NumberUtils.INTEGER_ZERO)));
    }

    /**
     * 根据id查询车型
     *
     * @param id
     * @return
     */
    @GetMapping("/queryCarModelById/{id}")
    public Result<CarModel> queryVehicleInfoById(@PathVariable("id") Long id) {
        return Result.success(carModelService.queryCarModelById(id));
    }


    /**
     * 添加车型
     *
     * @return
     */
    @PostMapping("/addCarModel")
    public Result<Boolean> addCarModel(@Valid @RequestBody CarModelDTO carModelDTO) {
        return Result.success(carModelService.addCarModel(carModelDTO.convertCarModel(carModelDTO, null)));
    }

    /**
     * 编辑车型
     *
     * @param carModelDTO
     * @return
     */
    @PostMapping("/updateCarModel")
    public Result<Boolean> UpdateCarModel(@Valid @RequestBody CarModelDTO carModelDTO, HttpServletRequest request) {
        return Result.success(carModelService.UpdateCarModel(carModelDTO.convertCarModel(carModelDTO, userNameThreadLocal.get())));
    }


    /**
     * 删除车型
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCarModelById/{id}")
    public Result<Boolean> deleteCarModelById(@PathVariable("id") Long id) {
        return Result.success(carModelService.deleteCarModelById(id));
    }


}

